package com.pocketsweet.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.ui.uilib.LoadingDailog;
import com.pocketsweet.ui.uilib.adapter.VoiceAdapter;
import com.pocketsweet.utils.ToolKits;

@ContentView(R.layout.activity_user_voice)
public class EditUserVoice extends BaseActivity implements OnClickListener {
	private HeaderLayout header;
	private VoiceAdapter voiceAdapter;
	private LoadingDailog loading;

	@ViewInject(R.id.voiceListView)
	private static ListView voiceListView;

	@ViewInject(R.id.linVoice)
	private static LinearLayout linVoice;

	@ViewInject(R.id.btnAddVoice)
	private static TextView btnAddVoice;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		initView();
		setHeaderFunction();
	}

	private BroadcastReceiver receiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(Main.ACTION_VOICE)) {
				voiceAdapter.notifyDataSetChanged();
				voiceListView.setAdapter(voiceAdapter);
				if (Main.voiceList.size() > 0) {
					// linVoice.setVisibility(View.VISIBLE);
					btnAddVoice
							.setBackgroundResource(R.drawable.shape_btn_unadd);
					btnAddVoice.setTextColor(getResources().getColor(
							R.color.text_999));
				} else {
					// linVoice.setVisibility(View.GONE);
					btnAddVoice
							.setBackgroundResource(R.drawable.shape_btn_login);
					btnAddVoice.setTextColor(getResources().getColor(
							R.color.white));
				}
			}
		}
	};

	@Override
	public void onPause() {
		super.onPause();
		if (Main.MediaTimer != null && Main.mMediaPlayer.isPlaying()) {
			Main.mMediaPlayer.pause();
			Main.mMediaPlayer.stop();
			Main.mMediaPlayer.reset();
			Main.MediaTimer.cancel();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(receiver);

	}

	@Override
	protected void onStart() {
		super.onStart();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Main.ACTION_VOICE);
		registerReceiver(receiver, filter);

	}

	@Override
	protected void onResume() {
		super.onResume();
		voiceListView.setAdapter(voiceAdapter);
	}

	private void initView() {
		btnAddVoice.setOnClickListener(this);
		if (Main.voiceList != null && Main.voiceList.size() > 0) {
			// Main.voiceList.clear();
			// linVoice.setVisibility(View.VISIBLE);
			// btnAddVoice.setVisibility(View.GONE);
			btnAddVoice.setBackgroundResource(R.drawable.shape_btn_unadd);
			btnAddVoice.setTextColor(getResources().getColor(R.color.text_999));
		} else {
			// linVoice.setVisibility(View.GONE);
			// btnAddVoice.setVisibility(View.VISIBLE);
			btnAddVoice.setBackgroundResource(R.drawable.shape_btn_login);
			btnAddVoice.setTextColor(getResources().getColor(R.color.white));
		}
		// Log.e("==voice", Main.voiceList.toString() + " ");
		voiceAdapter = new VoiceAdapter(EditUserVoice.this, Main.voiceList,
				true);
		voiceAdapter.notifyDataSetChanged();
		voiceListView.setAdapter(voiceAdapter);
		voiceListView.setCacheColorHint(0);
	}

	public void setHeaderFunction() {
		header = (HeaderLayout) findViewById(R.id.mainHeader);
		header.init(HeaderStyle.IMG_TITLE_TEXT);
		header.setMiddleText("声线");
		header.setRightText("");
		header.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				EditUserVoice.this.finish();
			}
		});
	}

	private void initTag() {
		Intent recordintent = new Intent(EditUserVoice.this, DialogAudio.class);
		EditUserVoice.this.startActivity(recordintent);
	}

	@Override
	public void onClick(View arg0) {
		if (Main.voiceList.size() > 0){
			ToolKits.toast(EditUserVoice.this, "已有声线");
			return;
		}
		initTag();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}
}